package com.qianfeng.openplatform.geteway.fiter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.qianfeng.openplatform.commons.field.RedisKeyPrefix;
import com.qianfeng.openplatform.commons.field.ResponseMessage;
import com.qianfeng.openplatform.commons.field.StaticField;
import com.qianfeng.openplatform.geteway.client.CacheClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
 * @author 江江
 * @Date 2020/7/27 14:19
 */
@Component
public class ServiceParamFilter extends ZuulFilter {
    @Autowired
    private CacheClient cacheClient;

    @Override
    public String filterType() { //指定当前过滤器的类型
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() { //指定过滤器的执行顺序
        return FilterConstants.PRE_DECORATION_FILTER_ORDER + 4;
    }

    @Override
    public boolean shouldFilter() {// 开启当前过滤器
        return RequestContext.getCurrentContext().sendZuulResponse();
    }

    @Override
    public Object run()throws ZuulException  {
        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();

        String method = request.getParameter("method");
        System.out.println("ServiceParamFilter 50"+method);
            try {
                if (!StringUtils.isEmpty(method)){
                Set<Object> members = cacheClient.sMembers(RedisKeyPrefix.WEB_MASTER_API_SERVICE_PARAM_KEY + method);
System.out.println("ServiceParamFilter 54:"+ members.size());
                if (members!=null){
                    for (Object m : members) {
                        String paramName = request.getParameter((String) m);
System.out.println("ServiceParamFilter 58:"+paramName);
                        if (StringUtils.isEmpty(paramName)){
                            System.out.println("ServiceParamFilter 59"+ m.toString());
                            throw new RuntimeException(m.toString());
                        }
                    }
                }
        }else{
            throw new Exception("not method param value error");
        }
    }catch (Exception e) {
            try {
                context.setSendZuulResponse(false);//拦截响应
                ObjectMapper objectMapper = new ObjectMapper();
                HttpServletResponse response = context.getResponse();
                response.setContentType("application/json;charset=utf-8");
                response.getWriter().write(objectMapper.writeValueAsString(new ResponseMessage(StaticField.REQEST_NOT_PARAM, "参数错误:"+e.getMessage())));
            } catch (IOException e2) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
